home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V2 / ShowINIT ƒ / ShowINIT.c < prev    next >
C/C++ Source or Header  |  1991-09-30  |  6KB  |  154 lines

  1. /*-------------------------------------------------------------------------
  2.   Filename: CShowINIT_PROC.c
  3.   Color ShowINIT, for use with LightspeedC
  4.   This translation by Ken McLeod (thecloud@dhw68k.cts.com)
  5.   Version of: Thursday, April 6, 1989 3:30:00 PM
  6.  
  7.   INIT notification routine
  8.   by Paul Mercer, Darin Adler, and Paul Snively from an idea by Steve Capps
  9.   Version of: Friday, July 15, 1988 12:08:09 AM    (1.1B1)
  10.     -revved back to previous calling interface.
  11.     -you only need to call ShowINIT now and due to popular demand,
  12.      deltaX is back!
  13.     -also due to popular demand, color icons are now done automatically.
  14.     -note that the color icon is only used if 4 bits or more is available on
  15.      the main graphics device; the normal #ICN is used for all other cases.
  16.   
  17.   Build & save this file as a 'PROC' resource, and include it in your
  18.   INIT's resource file.  Use the following code within your INIT to load
  19.   the 'PROC' and call CShowINIT:
  20.   
  21.       Handle    procH;
  22.       
  23.       if ((procH = GetResource('PROC', PROC_ID)) != 0L)    {
  24.             HLock(procH);
  25.             CallPascal(ICON_ID, -1, *procH);
  26.             HUnlock(procH);
  27.         }
  28.  
  29.   -------------------------------------------------------------------------*/
  30.  
  31. /* #include <MacHeaders> */
  32. /*#include <Color.h>*/
  33.  
  34. typedef struct QuickDraw {       /* struct to hold QuickDraw globals */
  35.   char private[76];
  36.   long randSeed;
  37.   BitMap screenBits;
  38.   Cursor arrow;
  39.   Pattern dkGray;
  40.   Pattern ltGray;
  41.   Pattern gray;
  42.   Pattern black;
  43.   Pattern white;
  44.   GrafPtr thePort;
  45. } QuickDraw;
  46.  
  47. /*extern short ROM85 : 0x28E;*/
  48. /*extern GDHandle MainDevice : 0x8A4;*/
  49.  
  50. extern short myH : 0x92C;        /* CurApName+28   */
  51. extern short myCheck: 0x92E;     /* CurApName+30   */
  52.  
  53. #define  firstX         8        /* left margin - offset to first icon */
  54. #define  bottomEdge     8        /* this far from bottom of screen */
  55. #define  iconWidth      32       /* size of icon (square normally) */
  56. #define  defaultMoveX   40       /* default amount to move icons */
  57. #define  checksumConst  0x1021   /* constant used for computing checksum */
  58. #define  minColorDepth  4        /* min. bits/pixel for drawing color icons */
  59. #define  maskOffset     128      /* offset to mask in ICN# resource */
  60. #define  iconRowBytes   32/8     /* 32/8 bits */
  61. #define  hasCQDBit      6        /* bit in ROM85 cleared if CQD available */
  62.  
  63. /*-------------------------------------------------------------------------
  64.   Display the ICN# (cicn when in 4 bit mode or higher) specified by iconID
  65.   and move the pen horizontally by moveX.  Pass a -1 in moveX to move the
  66.   standard amount (40 pixels).
  67.  
  68.   pascal void ShowINIT(iconID, moveX)
  69.     short iconID, moveX;
  70.     extern;
  71.  
  72.   -------------------------------------------------------------------------*/
  73.  
  74. pascal void main(iconID, moveX)
  75. short iconID, moveX;
  76. {
  77.   Handle  theIconHdl;                      /* handle to the icon (or cicn) */
  78.   short     dh;                         /* for calculating horizontal offset */
  79.   short  colorFlag;             /* set if drawing a color icon */
  80.   short  theDepth;              /* depth of main screen; used for CQD only */
  81.   GDHandle theMainDevice;       /* handle to main screen device; CQD only */
  82.   Rect srcRect, destRect;       /* source & destination rectangles */
  83.   BitMap myBitMap;              /* icon bitmap; used for b/w icon only */
  84.   GrafPort myPort;              /* port we draw into */
  85.   QuickDraw qdGlobals;             /* our own personal QD globals... */
  86.   Ptr localA5;                  /* pointer to qdGlobals.thePort */
  87.   Ptr    savedA5;                                    /* storage for saved contents of A5 */
  88.   
  89.   asm {
  90.     move.l  A5,savedA5                    /* save "real" QD globals ptr */
  91.     lea          localA5,A5                    /* set up A5 to point to our globals */
  92.     move.l  A5,CurrentA5
  93.   }
  94.   InitGraf(&qdGlobals.thePort);    /* initialize our qdGlobals structure */
  95.   OpenPort(&myPort);
  96.   colorFlag = 0;                                /* default: no color */
  97.  
  98.   if (!(BitTst(&ROM85, 7-hasCQDBit))) {    /* does CQD exist? */
  99.     theMainDevice = MainDevice;                    /* yes; get handle to main device */
  100.         theDepth = (*(*theMainDevice)->gdPMap)->pixelSize;
  101.     if (theDepth >= minColorDepth)    {        /* deep enough to draw in color? */
  102.       if ((theIconHdl = (Handle)GetCIcon(iconID)) != 0L)
  103.         colorFlag = 1;                                    /* found a color icon; set flag */
  104.     }
  105.   }
  106.  
  107.   if (!(colorFlag))    {  /* no CQD, insufficient depth, or lack of 'cicn' */
  108.     if (!(theIconHdl = GetResource('ICN#',iconID))) {
  109.       SysBeep(3);  /* can't get b/w icon; signal error and bail out */
  110.       goto out;
  111.     }
  112.   }
  113.   dh = (myH << 1) ^ checksumConst;           /* checksum to find dh */
  114.   myH = ((dh == myCheck) ? (myH):(firstX));  /* reset if necessary */
  115.   /* notice that we stored the new horizontal value directly back into
  116.       the low-memory 'myH' location, rather than using a temporary variable.
  117.       This is the way the original ShowINIT works, and IconWrap relies on it. */
  118.  
  119.   destRect.bottom = myPort.portRect.bottom - bottomEdge;
  120.   destRect.left = myPort.portRect.left + myH;
  121.   destRect.top = destRect.bottom - iconWidth;
  122.   destRect.right = destRect.left + iconWidth;
  123.  
  124.   if (colorFlag) {                                /* draw color icon */
  125.       PlotCIcon(&destRect,(CIconHandle)theIconHdl);
  126.     DisposCIcon((CIconHandle)theIconHdl);
  127.   }
  128.   else {                                          /* draw b/w icon */
  129.     HLock(theIconHdl);
  130.     srcRect.top = srcRect.left = 0;
  131.     srcRect.bottom = srcRect.right = iconWidth;
  132.     myBitMap.rowBytes = iconRowBytes;
  133.     myBitMap.bounds = srcRect;
  134.     myBitMap.baseAddr = *theIconHdl + maskOffset; /* punch hole with mask */
  135.     CopyBits(&myBitMap, &myPort.portBits, &srcRect, &destRect, srcBic, 0L);
  136.     myBitMap.baseAddr = *theIconHdl;              /* now draw the icon */
  137.     CopyBits(&myBitMap, &myPort.portBits, &srcRect, &destRect, srcOr, 0L);
  138.     HUnlock(theIconHdl);
  139.     ReleaseResource(theIconHdl);
  140.   }
  141.   myH += ((moveX == -1) ? (defaultMoveX):(moveX));  /* advance for next time */
  142.   myCheck = (myH << 1) ^ checksumConst;             /* calc new checksum */
  143.  
  144. out:
  145.   ClosePort(&myPort);
  146.   asm {
  147.     move.l  savedA5,A5
  148.     move.l  A5,CurrentA5
  149.   }
  150.  
  151. }
  152.  
  153. /*-------------------------------------------------------------------------*/
  154.